package com.javasm.dao.MisMannageDao.impl;


import com.javasm.dao.MisMannageDao.AreaDao;
import com.javasm.entity.MisMannageEntity.AreaInfo;
import com.javasm.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/23 14:15
 * @version:0.1
 * @since:jdk11
 */
public class AreaDaoImpl implements AreaDao {

    @Override
    public String getAreaName(Integer queryAreaId) {
        Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        StringBuilder sb = new StringBuilder();
        sb.append("select ta.area_name from tb_area ta where ta.area_id=?");

        String areaName = null;
        try {
            ps = conn.prepareStatement(sb.toString());
            ps.setInt(1,queryAreaId);
            rs = ps.executeQuery();
            while (rs.next()) {
                areaName = rs.getString("area_name");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeConn(conn, ps,null, rs);
        }
        return areaName;
    }

    @Override
    public List<AreaInfo> getAllArea(Integer queryAreaPid) {
        Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        StringBuilder sb = new StringBuilder();
        sb.append("select ta.area_id,ta.area_name,ta.area_pid from tb_area ta where ta.area_pid=?");

        List<AreaInfo> provList = new ArrayList<AreaInfo>();
        try {
            ps = conn.prepareStatement(sb.toString());
            ps.setInt(1,queryAreaPid);
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer areaId = rs.getInt("area_id");
                String areaName = rs.getString("area_name");
                Integer areaPid = rs.getInt("area_pid");
                AreaInfo areaInfo = new AreaInfo();
                areaInfo.setAreaId(areaId);
                areaInfo.setAreaName(areaName);
                areaInfo.setAreaPid(areaPid);
                provList.add(areaInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeConn(conn, ps,null, rs);
        }
        Map map = new HashMap();
        map.put(1,1);
        return provList;
    }
}
